
using M0;
using UnityEngine;

public class 单选按钮 : GameButton
{
    [SerializeField]private string Key;
    public int Index;
    public GameObject selectObj;
    public GameObject unSelectObj;

    public void 更新Key(string str)
    {
        if (!string.IsNullOrEmpty(Key))
        {
            MyEvent.Instance.RemoveEvent<int>(Key,单向Event);
        }
        Key = str;
        MyEvent.Instance.AddEvent<int>(Key,单向Event);
    }
    
    private void OnEnable()
    {
        if (!string.IsNullOrEmpty(Key))
        {
            MyEvent.Instance.AddEvent<int>(Key,单向Event);
        }
        
        OnClickEvent.AddListener(单选OnClick);
    }

    public void 初始化未选()
    {
        单向Event(-1);
    }
   
    
    private void 单选OnClick()
    {
        MyEvent.Instance.DoEvent(Key,Index);
    }

    private void OnDisable()
    {
        if (!string.IsNullOrEmpty(Key))
        {
            MyEvent.Instance.RemoveEvent<int>(Key,单向Event);
        }
        OnClickEvent.RemoveListener(单选OnClick);
    }

    private void 单向Event(int i)
    {
        bool 选中 = Index == i;
        if(selectObj!= null) selectObj.SetActive(选中);
        if(unSelectObj!= null) unSelectObj.SetActive(!选中);
    }
}
